package cn.iocoder.yudao.module.custom.service.county;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.custom.controller.admin.county.vo.*;
import cn.iocoder.yudao.module.custom.dal.dataobject.county.CountyDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.custom.convert.county.CountyConvert;
import cn.iocoder.yudao.module.custom.dal.mysql.county.CountyMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.custom.enums.ErrorCodeConstants.*;

/**
 * 区县信息 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class CountyServiceImpl implements CountyService {

    @Resource
    private CountyMapper countyMapper;

    @Override
    public Integer createCounty(CountyCreateReqVO createReqVO) {
        // 插入
        CountyDO county = CountyConvert.INSTANCE.convert(createReqVO);
        countyMapper.insert(county);
        // 返回
        return county.getId();
    }

    @Override
    public void updateCounty(CountyUpdateReqVO updateReqVO) {
        // 校验存在
        validateCountyExists(updateReqVO.getId());
        // 更新
        CountyDO updateObj = CountyConvert.INSTANCE.convert(updateReqVO);
        countyMapper.updateById(updateObj);
    }

    @Override
    public void deleteCounty(Integer id) {
        // 校验存在
        validateCountyExists(id);
        // 删除
        countyMapper.deleteById(id);
    }

    private void validateCountyExists(Integer id) {
        if (countyMapper.selectById(id) == null) {
            throw exception(COUNTY_NOT_EXISTS);
        }
    }

    @Override
    public CountyDO getCounty(Integer id) {
        return countyMapper.selectById(id);
    }

    @Override
    public List<CountyDO> getCountyList(Collection<Integer> ids) {
        return countyMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<CountyDO> getCountyPage(CountyPageReqVO pageReqVO) {
        return countyMapper.selectPage(pageReqVO);
    }

    @Override
    public List<CountyDO> getCountyList(CountyExportReqVO exportReqVO) {
        return countyMapper.selectList(exportReqVO);
    }

    @Override
    public List<CountyDO> getCountyLists(Integer parentId) {
        return countyMapper.getCountyLists(parentId);
    }

}
